/*
 * @Description: MT3Y顶层接口
 * @Version: 0.1
 * @Autor: 宣某
 * @Date: 2021-02-23 11:43:45
 * @FilePath: \MT500-EM-A39E17-2022-01-17\System\Protocol\Protocol.c
 * @LastEditors: 许振富(jia)
 * @LastEditTime: 2022-02-25 13:55:31
 */
#include "Protocol.h"
#include "string.h"
#include "../ASM_Delay/ASM_Delay.h"

#include "RTT_Log.h"

//接收并处理完成的数据缓冲区
__protocol_buf Protocol_Rx_Buffer =
    {
        .Length = 0,
        .Cmd = 0};
/* 备份数据 */
//__tx_Backup Protocol_Tx_Backup =
//    {
//        .Sta = 0,
//		.Data ={0},
//        .Len = 0};
//数据发送缓冲
__tx_buf Protocol_Tx_Buffer =
    {
        .Length = 0};
	
unsigned char USB_KB_Enum = 0;
extern volatile unsigned int SysTickNum;
volatile unsigned int Start_Tick;
/**
 * @description: 查询协议栈接收完成函数
 * @param {*}
 * @return {unsigned char} 0:未接收完成 1:接收完成
 * @author: 宣某
 */
unsigned char Protocol_GetValid(void)
{	
#if USB_EN
    //查询USB栈
    if (USB_Protocol.Flag.Command_Valid_Flag== 1)
	{
		DeBug_Log("->cmd:0x%04X-tick:%d\n",Protocol_Rx_Buffer.Cmd,SysTickNum);
		Start_Tick = SysTickNum;
		memset((void*)&Protocol_Tx_Buffer,0,sizeof(__tx_buf));   
        return 1;
	}
#endif
#if UART_EN
    //查询Uart栈
    if (Uart_protocol.Flag == Valid)
	{
		memset((void*)&Protocol_Tx_Buffer,0,sizeof(__tx_buf)); 
        return 1;
	}
#endif
    return 0;
}
/**
 * @description: 协议栈数据回报函数
 * @param {unsignedshort} _Sta:返回状态
 * @param {unsignedchar} *_Buf:返回数据
 * @param {unsignedshort} _Len:返回数据长度
 * @return {*}
 * @author: 宣某
 */
void Protocol_RePort(unsigned short _Sta, unsigned char *_Buf, unsigned short _Len)
{
	unsigned int Run_Tick;
#if USB_EN
    //若为USB接收完成的,就由USB返回
    if (USB_Protocol.Flag.Command_Valid_Flag == 1)
    {
		Run_Tick = SysTickNum-Start_Tick;
		DeBug_Log("->sta:0x%04X-tick:%d-Run:%d\n",_Sta,SysTickNum,Run_Tick);
//		if(Run_Tick < 30)
//			ASM_DELAY_MS(30-Run_Tick);
        USB_Protocol.Tx_Handler(_Sta, _Buf, _Len);
		
        /* 备份数据 */
//        Protocol_Tx_Backup.Sta = _Sta;
//        Protocol_Tx_Backup.Len = _Len;
//        memcpy(Protocol_Tx_Backup.Data,_Buf, _Len);

		memset((void*)&Protocol_Rx_Buffer,0,sizeof(__protocol_buf));
		USB_Protocol.Flag.Command_Valid_Flag = 0;
        return;
    }
#endif
#if UART_EN
    //若为Uart接收完成的,就由Uart返回
    if (Uart_protocol.Flag == Valid)
    {
        Uart_protocol.Transmit_Handler(_Sta, _Buf, _Len);
		memset((void*)&Protocol_Rx_Buffer,0,sizeof(__protocol_buf));
        return;
    }
#endif
//	USB_Protocol.Tx_Handler(Protocol_Tx_Backup.Sta, Protocol_Tx_Backup.Data, Protocol_Tx_Backup.Len);
}




